function loadfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

var Style = {
	getComputed: function(elem, style) {
	  var computedStyle;
	  if (typeof elem.currentStyle != 'undefined')
	     computedStyle = elem.currentStyle;
	  else
	     computedStyle = document.defaultView.getComputedStyle(elem, null);
	  return computedStyle[style];
	},
	hasParentClass: function(elem, cname) {
	    var upper = $(elem);
	    do {
	       if(typeof upper.hasClassName == "function" && upper.hasClassName(cname))
	           return true;
	    } while((upper = $(upper.parentNode || upper.up())) != null);
	    return false;
	}
};
Element.addMethods(Style);

var Select = {
    selectDrop: function(element, val) {
        if(!element || !element.select) return;
        if(typeof val == "undefined") val = !element.droppedDown;
        element.droppedDown = val;
        element.select.button.innerHTML = element.opts.arrows[val ? 1 : 0];
        if(val) {
            element.select.addClassName("ieSelected");
            Select.dropDown(element);
        } else {
            element.select.removeClassName("ieSelected");
            if(element.select.dropdown)
                element.select.dropdown.remove();
        }
    },
    dropText: function(element, text) {
        if(typeof text == "undefined") text = $(element).getValue();
        element.select.contentFree.innerHTML = text;
        element.select.content.setValue(text);
    },
    dropDown: function(element) {
        if(!element.select.dropdown.childElements().length) {
            var elems = 0;
            element.select.dropdown.observe("mouseover", function() {
               element.onSelection = true; 
            }).observe("mouseout", function() {
               element.onSelection = false; 
            });
    		$(element).childElements().each(function(el) {
    		    var li = $(document.createElement("li"))
    		    .setStyle({
    		        listStyleType:"none"
    		    }).observe("click", function() {
    		        element.lastSelected.removeClassName("selected");
    		        element.selectedIndex = li.index;
    		        element.lastSelected = li.addClassName("selected");
    		        Select.dropText(element);
    		        element.onSelection = false;
    		        Select.selectDrop(element, false);
    		        element.select.content.fire("focus");
    		    });
    	        li.index = elems++;
    	        if(li.index == element.selectedIndex)
    	            element.lastSelected = li.addClassName("selected");
    		    li.innerHTML = el.innerHTML;
    		    element.select.dropdown.insert(li);
    		});
	    }
		element.select.insert({after:element.select.dropdown});
    },
    selectIt: function(element, opts) {
        var pr = $(element);
		var defaults = {
		    arrows: ["&#x25be;", "&#x25b4;"],
		    hideOn: "blur",
		    showOn: "click"
		};
		element.opts = opts = Object.extend(defaults, opts);
		var select = $(document.createElement("div"))
		.writeAttribute("style", pr.readAttribute("style"))
		.addClassName("ieSelect")
		.writeAttribute("tabindex", "0")
		.setStyle({
		    width:pr.getWidth() + "px",
			cursor:"pointer"
    	});
    	pr.classNames().each(function(el) { select.addClassName(el)	});
    	contentStyler = function(el) {
       		el.setStyle({
    			margin:"1px",
    			overflow:"hidden",
    			position:"absolute"
    		});
        }
		var content = $(document.createElement("input"))
		.writeAttribute("type", "text")
		.addClassName("content").addClassName("focused");
		contentStyler(content);
		var contentFree = $(document.createElement("p"))
		.addClassName("content");
		contentStyler(contentFree);
		var button = $(document.createElement("p"))
		.addClassName("button")
		.setStyle({
			margin:"0",
			float:"right"
		});
		select.button = button;
		select.content = content;
		select.contentFree = contentFree;
		var dropdown = $(document.createElement("ul"))
		.addClassName("ieDropdown")
		.setStyle({
		    margin:"0", padding:"0",
		    border:"solid 1px",
		    position:"absolute"
		});
		select.insert(contentFree).insert(button);
		element.select=select;
		pr.insert({after: select});
		Select.selectDrop(element, false);
		Select.dropText(element);
		element.select.dropdown=dropdown;
		var focusSelect = function(evt) {
		    if(contentFree.up())
			    contentFree.insert({before:content}).remove();
		}
		var focusLost = function(evt) {
		    content.insert({before:contentFree}).remove();
		}
		select.observe(opts.showOn, function(evt) {
			Select.selectDrop(element);
			focusSelect(evt);
		}).observe(opts.hideOn, function(evt) {
			if(!element.droppedDown || element.onSelection) return;
			Select.selectDrop(element, false);
		})
		if(Prototype.Browser.Opera)
		    content.observe("focus", focusSelect)
		        .observe("blur", focusLost);
		select.observe("focus", focusSelect)
		    .observe("blur", focusLost);
		// TODO: -8 looks better.. why?
		var w = select.getWidth() - button.getWidth() - 8 + "px";
		content.setStyle({width:w});
		contentFree.setStyle({width:w});
		return pr.hide();
		//return pr;
    }
}
Element.addMethods(Select);
